<?php
/**
 * 权限角色管理
 */

namespace app\backend\components;

use Yii;

class AppAdminAcl
{
//权限配制数据
    public static $aclList = [
        [
            'name'=>'市场',
            'module'=>'market',
            'ctl'=>[
                [
                    'name'=>'市场',
                    'icon'=>'icon-user-following',
                    'list_ctl'=>['marketinfo', 'project'],
                    'act'=>[
                        'marketinfo'=>[
                            'name'=>'市场信息',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'市场信息']
                        ],
                        'project'=>[
                            'name'=>'项目列表',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'项目列表'],
                        ],
                    ],
                ],
            ]
        ],
        [
            'name'=>'银行',
            'module'=>'bank',
            'ctl'=>[
                [
                    'name'=>'银行',
                    'icon'=>'icon-wallet',
                    'list_ctl'=>['bankinfo','bankaccount'],
                    'act'=>[
                        'bankinfo'=>[
                            'name'=>'基本资料',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'基本资料']
                        ],
                        'bankaccount'=>[
                            'name'=>'账户',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'账户列表']
                        ],
                    ],
                ],
            ]
        ],

        [
            'name'=>'用户',
            'module'=>'user',
            'ctl'=>[
                [
                    'name'=>'用户',
                    'icon'=>'icon-puzzle',
                    'list_ctl'=>['userinfo','roleinfo'],
                    'act'=>[
                        'userinfo'=>[
                            'name'=>'用户资料',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'用户资料'],
                        ],
                        'roleinfo'=>[
                            'name'=>'用户角色',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'用户角色'],
                        ],
                    ]
                ],

            ]
        ],


        [
            'name'=>'系统',
            'module'=>'desktop',
            'ctl'=>[
                [
                    'name'=>'系统',
                    'icon'=>'icon-settings',
                    'list_ctl'=>['admin','role'],
                    'act'=>[
                        'admin'=>[
                            'name'=>'系统管理员管理',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'操作员列表','create'=>'操作员创建','update'=>'操作员编辑','cancel'=>'操作员冻结','open'=>'操作员解冻']
                        ],
                        'role'=>[
                            'name'=>'系统角色管理',
                            'sidebar'=>true,
                            'default_id'=>'index',
                            'list_act'=>['index'=>'角色列表','create'=>'角色创建','update'=>'角色编辑','cancel'=>'角色冻结','open'=>'角色解冻']
                        ],
                    ]
                ],
            ]
        ],
    ];

    /**
     * 后台菜单过滤
     *
     * @param $acl_list
     * @param $super
     * @return array
     */
    public static function filterMenu($acl_list,$super)
    {
        $item = self::$aclList;
        if ($super == 1) return $item;
        foreach ($item as $k=>$v) {
            foreach ($v['ctl'] as $kk=>$vv) {
                foreach ($vv['act'] as $kkk=>$vvv) {
                    $acl = $v['module'].'/'.$kkk.'/'.$vvv['default_id'];
                    if (strpos($acl_list, $acl) === false) {
                        unset($item[$k]['ctl'][$kk]['act'][$kkk]);
                    }
                }
                if (empty($item[$k]['ctl'][$kk]['act'])) unset($item[$k]['ctl'][$kk]);
            }
            if (empty($item[$k]['ctl'])) unset($item[$k]);
        }
        return $item;
    }

    /**
     * 判断按钮是否有权限
     *
     * @param $act
     * @param $button
     * @return string
     */
    public static function filterButton($act, $button = true)
    {
        if (Yii::$app->session['super'] == 1) return $button;
        if (strpos(Yii::$app->session['acl'], $act) !== false) return $button;
        return '';
    }
}